package com.jeehentor.common.api.vo;

import com.alibaba.fastjson2.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serial;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户信息
 * @author wzd
 */
@Data
public class LoginUser implements UserDetails {

	@Serial
	private static final long serialVersionUID = 2317674540545112144L;
	@Schema(description = "主键id")
	private String id;

	@Schema(description = "登录账号")
	private String username;

	@Schema(description = "真实姓名")
	private String realname;

	@Schema(description = "密码")
	private String password;

	@Schema(description = "头像")
	private String avatar;

	@Schema(description = "生日")
	@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private LocalDateTime birthday;

	@Schema(description = "性别(0-默认未知,1-男,2-女)")
	private String sex;

	@Schema(description = "电子邮件")
	private String email;

	@Schema(description = "电话")
	private String phone;

	@Schema(description = "机构编码")
	private String orgCode;

	@Schema(description = "状态(1-正常,2-冻结)")
	private String status;

	@Schema(description = "删除状态(0-正常,1-已删除)")
	private String delFlag;

	@Schema(description = "创建人")
	private String createBy;

	@Schema(description = "创建时间")
	@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private LocalDateTime createTime;

	@Schema(description = "更新人")
	private String updateBy;

	@Schema(description = "更新时间")
	@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private LocalDateTime updateTime;

	/**
	 * 用户角色
	 */
	private List<String> roles;
	/**
	 * 用户角色
	 */
	private List<String> rolesLabel;

	/**
	 * 权限列表
	 */
	private List<String> permissions;

	/**
	 * 签发时间
	 */
	@JSONField(name = "iat")
	private Long iat;

	/**
	 * 过期时间
	 */
	@JSONField(name = "exp")
	private Long exp;

	/**
	 * JWT ID
	 */
	@JSONField(name = "jti")
	private String jti;

	private String originalPwd;

	@Override
	@JsonIgnore
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}
}
